/**
    ==================================================================================================
**    date: 2007.4.25
**    author: kuanglingxuan
**    description: show message tips by ajax
**  ==================================================================================================
**/


/**
*	class Dialog and MsgNode
*/
function Dialog(){

	this.msgQueue = [];
	this.size = 1;
	this.position = 1;
	this.width;
	this.height;
	this.timer;
	this.div;
	this.url;
	this.bkUrl;
	this.titleKey;
	this.closeKey;
	this.forwardKey;
	this.backwardKey;
	this.remindKey;
	this.remindTime;
	this.isWinOpen;
	this.isStep;
	this.stepTimer;
	this.closing=false;
}

function MsgNode(){
	this.msgId;
	this.url;
	this.msgText;
	this.dateTime;
}

/**
*  create div
*/
Dialog.prototype.popup = function(sHtml){
	var _sHtml   = sHtml  || this.sHtml;
	var	d        = document.createElement("div");

		d.id         = "dialog";
		d.innerHTML  = _sHtml;
		document.body.appendChild(d);
	//	alert('popup'+document.body);

	with(d.style){
	BORDERTOP = '#a6b4cf';
	BORDERRIGHT = '#455690';
	BORDERLEFT = '#a6b4cf';
	BORDERBOTTOM = '#455690';
	BACKGROUNDCOLOR = '#c9d3f3';
	position =  "absolute";
    zIndex   =  "99999";
	if(navigator.userAgent.indexOf("MSIE")>0){
		width    =  this.width+"px";
		height   =  this.height+"px";
	}
	if(navigator.userAgent.indexOf("Firefox")>0){

        width    =  this.width+"px";
		height   =  this.height+16+"px";
	}
//	alert(width);alert(height);
	left     =  document.body.clientWidth-d.clientWidth+"px";
//			alert(this.isStep);
	if(this.isStep == true){
//		alert('jjj'+this.isStep);
		top = document.body.clientHeight+"px";
	}else{
		top = document.body.clientHeight-d.clientHeight+"px";
	}
	display = 'none';
	}
	this.div=d; //keep this div is unique
//	alert('herer');

}

/**
*	show msg tip by step
*/
Dialog.prototype.stepTo = function(up){
//	alert('herer');
    var d = this.div;
    var tf;
    var elsedo;
    var x;
	if(up){
		if(this.closing == false){
	        tf = parseInt(d.style.top)>=document.body.clientHeight-d.clientHeight+5;
			x  = -5;
			elsedo = "";
		}
	}else{
		this.closing = true;
	     tf     = parseInt(d.style.top)<=document.body.clientHeight;
		 x      = 5;
		 elsedo = " dialog.closing = false;d.style.display = 'none';clearTimeout(dialog.stepTimer);";
	}

	if(tf){
	//	alert('dis');
		d.style.top  = parseInt(d.style.top)+x+"px";
		this.stepTimer = setTimeout("dialog.stepTo("+up+");",20);

	}else{
	//	alert('eslse');
		eval(elsedo);
	}
}

/**
*  move div when window resize or scroll
*/
Dialog.prototype.move = function(){

	var d = document.getElementById("dialog");
	if(d == null)
		return;
//	alert(this.div);
	with(d.style){
//	alert(document.body.scrollTop);
//	alert(document.body.scrollLeft);
	left    = document.body.clientWidth - d.clientWidth +document.body.scrollLeft+"px";
	top     = document.body.clientHeight - d.clientHeight+document.body.scrollTop+"px";
	}
}

/**
*	onforward method is invoked while click ">" button
*/
Dialog.prototype.onforward = function(){
//	alert('onforward');
	if(this.position < this.size){
		this.position += 1;
		this.onchange();
	}
	//alert("onforward"+this.position);
}

/**
*	onbackward method is invoked while click "<" button
*/
Dialog.prototype.onbackward = function(){

	if(this.position > 1){
		this.position -= 1;
		this.onchange();
	}
	//alert("onbackword"+this.position);
}

/**
*	onchange message
*/
Dialog.prototype.onchange =  function(){
//	alert(this.position);
	var msg = document.getElementById("msg");
	var dateTime = document.getElementById("dateTime");
	msg.innerHTML = this.msgQueue[this.position - 1].msgText;//match array index
	dateTime.innerHTML = this.msgQueue[this.position - 1].dateTime;
	//change position
	var position = document.getElementById("position");
	position.innerHTML = this.position + "/" + this.size;
}

/**
*	div string
*/
Dialog.prototype.divstr =  function(){
	var str = ""
        str += '<table width="100%" height="100%" border="0" cellpadding="1" cellspacing="1" bordercolor="#262752" bgcolor="262752">'
        str += "<TR>"
     //   str += "<TD style='FONT-SIZE: 12px;COLOR: #0f2c8c' width=30 height=24></TD>"
        str += '<TD class="msgtips_header"><table width="100%"><tr width="100%"><td align="left" width="70%"><span style="color:white" >' + this.titleKey + "</span></TD>"
        str += '<td width="30%" align="right">'
        str += "<SPAN title='"+this.closeKey+"' style='CURSOR: pointer;' id='close' >&nbsp;&nbsp;&nbsp;&nbsp;</SPAN></TD></tr></table></td>"
        str += "</TR>"
        str += "<TR>"
        str += "<TD bgcolor='#FFFFFF' >"
        str += "<DIV class='msgtips_text_div'>"
        str += "<DIV style='WORD-BREAK: break-all;width: 100%;height: 50' align=left>"
        str += "<SPAN id='dateTime'></SPAN><br>"

        str += "<A id='link'><FONT color=#0000ff>"

		str +="<SPAN style='CURSOR: pointer' id='msg'>"+ "no new message!"  + "</SPAN>"

		str += "</FONT></A></DIV>"

        str += "</DIV>"

		str += "<DIV style='WORD-BREAK: break-all' align=right>"
	//	alert(this.remindTime);
		if(this.remindTime != undefined){
			str += "<SPAN style='FONT-WEIGHT: ; FONT-SIZE: 12px; CURSOR: pointer; COLOR: blue; MARGIN-RIGHT: 4px' id='remind'>"+this.remindKey+"</SPAN>&nbsp;&nbsp;&nbsp;&nbsp;"
		}
		str += "<SPAN style='FONT-WEIGHT: ; FONT-SIZE: 12px; COLOR: red; MARGIN-RIGHT: 4px' id='position' > "+ this.position+ "/" +this.size + "</SPAN>"

		str += "<SPAN title='"+this.forwardKey+"' class='msgtips_arrow_left'  id='backward'>&nbsp;&nbsp;&nbsp;</SPAN>&nbsp;"

		str += "<SPAN title='"+this.backwardKey+"' class='msgtips_arrow_right' id='forward'>&nbsp;&nbsp;&nbsp;</SPAN>"

		str += "&nbsp;&nbsp;</DIV>"
        str += "</TD>"
        str += "</TR>"

        str += "</TABLE>"
	//	alert(str);


		return str;
}

Dialog.prototype.onlink = function(){
//	alert('link'+this.msgQueue[this.position - 1].url);
	if(this.isWinOpen == true)
		window.open(this.msgQueue[this.position - 1].url);
	else{
	//	window.location.href=this.msgQueue[this.position - 1].url;
		jumpToMenuPage(this.msgQueue[this.position - 1].url);
	}
}

/**
*	display div
*/
Dialog.prototype.ondisplay = function(){

	this.div.style.display = '';
	if(this.isStep == true){
			//		alert('heer-->'+dialog.isStep);
		this.stepTimer = setTimeout("dialog.stepTo(1);",100);
	}
}

Dialog.prototype.ondisappear = function(){
	var d = this.div;
	if(this.isStep == true)
		this.stepTo();//disappear	by step
	else
		d.style.display = 'none';

	ajaxSendReadFlag(this);
}
/**
*	remove ""
*/
Dialog.prototype.formatStrMark = function (sArg) {
	if(sArg != null) {
		if (sArg.indexOf("\"") == 0) sArg = sArg.substring(1);
		if (sArg.substring(sArg.length - 1) == "\"") sArg = sArg.substring(0, sArg.length - 1);
	}
	return sArg;
}

/**
*	parse msgstr to MsgNode
*/
Dialog.prototype.createMsgList = function(msgstr){
//	alert(msgstr);
//	msgstr = '"asdfasdf"|"hhhhhhh"';
	var msgList = msgstr.split("|");

	for (var i = 0; i < msgList.length; i++) {
		var msgNode = new MsgNode();
		var tmp = msgList[i];
	//	alert(msgList[i]);
		var tmpArray = tmp.split(",");
		for(var j = 0; j < tmpArray.length; j++){

			tmpArray[j] = this.formatStrMark(tmpArray[j]);
	//		alert(tmpArray[j]);
		}
		msgNode.msgText = tmpArray[0];
		msgNode.url = tmpArray[1];
		msgNode.dateTime = tmpArray[2];
		this.msgQueue[i] = msgNode;
	}
	this.size = this.msgQueue.length;
//	alert(this.msgQueue.length);
}

/**
*	parse msgList xml to MsgNode
*/
Dialog.prototype.parseXmlMsgList = function(xml){

		var xmlEle = xml.documentElement;
	//	alert(xmlEle)
		var dataList = new Array();
		if (xmlEle) dataList = xmlEle.getElementsByTagName('msg-node');

		var dataLen = dataList.length;
	//	alert(dataLen)
		if(dataLen == 0) return 'false';
		this.msgQueue = [];
		for (i = 0; i < dataLen; i++) {

			var msgNode = new MsgNode();
			msgNode.msgId = dataList[i].childNodes[0].firstChild.nodeValue;
			msgNode.msgText = dataList[i].childNodes[1].firstChild.nodeValue;//.firstChild;
	//		alert(dataList[i].childNodes[0].firstChild.nodeValue);
	//		alert(dataList[i].childNodes.length);
			msgNode.url = dataList[i].childNodes[2].firstChild.nodeValue;
	//		alert(msgNode.url);
			msgNode.dateTime = dataList[i].childNodes[3].firstChild.nodeValue;
	//		alert(msgNode.dateTime);
			this.msgQueue[i] = msgNode;

		}
		this.size = this.msgQueue.length;
		return 'true';
}

Dialog.prototype.listeners = function(){
//register methods
	var onforward = document.getElementById("forward");
	onforward.onclick = function(){
		dialog.onforward();

	}
	var onbackward = document.getElementById("backward");
		onbackward.onclick = function(){
		dialog.onbackward();
	}
	var onclose = document.getElementById("close");
	onclose.onclick = function(){
		dialog.ondisappear();
	}
	var onlink = document.getElementById("link");
	onlink.onclick = function(){

		dialog.onlink();
	}

	if(this.remindTime != undefined){
		var onremind = document.getElementById("remind");
		onremind.onclick = function(){
			dialog.ondisappear();
	//		alert(dialog.remindTime);
			setTimeout("dialog.ondisplay()",dialog.remindTime-0); //-0 for change string to number
		}
	}
	//register methods
}

/**
*  ajax part
*/

	 function send_request(dialog){
	     var http_request=false;

	     if(window.XMLHttpRequest){//Mozilla
	         http_request=new XMLHttpRequest();
	         if(http_request.overrideMimeType){
	             http_request.overrideMimeType("text/xml");
	         }
	     }
	     else if(window.ActiveXObject){//IE
	         try{
	             http_request=new ActiveXObject("Msxml2.XMLHTTP");
	         }
	         catch(e){
	             try{
	                 http_request=new ActiveXObject("Microsoft.XMLHTTP");
	             }
	             catch(e){}
	         }
	     }

	     if(!http_request){
	         window.alert("can't create XMLHttpRequest!");
	         return false;
	     }

	     http_request.onreadystatechange=function(){processRequest(http_request,dialog)};

	     http_request.open("GET",dialog.url,true);
	     http_request.send(null);
	 }

	 function processRequest(http_request,dialog){

	     if(http_request.readyState==4){

	         if(http_request.status==200){
			//	alert(http_request);
				var str = http_request.responseText;
				var xml = http_request.responseXML;
			//	alert(str.length);

				if(xml != null && str.length > 0){
			//	alert(str);
			//	dialog.createMsgList(str);
				var flag = dialog.parseXmlMsgList(xml);
			//	alert(flag)
				if(flag == 'true'){//it's a correct xml
					if(dialog.div == null || dialog.div == undefined){//if div is null then create a new div
						dialog.popup(dialog.divstr());
						dialog.listeners();
					}
					//alert(dialog.position);
					dialog.position = 1;
					//alert(dialog.position);
					dialog.onchange();

				//	dialog.div.style.display = '';

					dialog.ondisplay();
				}
				}

	         }

	     }
	 }

	/**
	 * close dialog then send read flag
	 */
	function ajaxSendReadFlag(dialog){
		var ids='';
		for(var i=0;i<dialog.msgQueue.length;i++){
		//	alert(dialog.msgQueue[i].msgId);
			ids += dialog.msgQueue[i].msgId+";";
		}
		dialog.url = dialog.url+"&ids="+ids;
		send_request(dialog);
	}

	/**
	 * timer send request
	 */
	function ajaxSendRequest(dialog){
	//	alert('herer');
		if(dialog.bkUrl == undefined){
			dialog.bkUrl = dialog.url+"&time="+new Date().getTime();
		}else{
			dialog.url = dialog.bkUrl;
		}
	//	alert('ajaxSendRequest-->'+dialog.url);

		send_request(dialog);

	}
/*
	function ajaxReFresh(dialog){
		alert('herer');
	//	send_request(dialog);
			var str = 'mytest,2006'//http_request.responseText;
			//	alert(str.length);
				if(str.length > 0){
			//	alert(str);
				dialog.createMsgList(str);

				if(dialog.div == null || dialog.div == undefined){
					dialog.popup(dialog.divstr());
					dialog.listeners();
				}
				dialog.onchange();
			//	dialog.div.style.display = '';

					dialog.ondisplay();

				}
	}
*/
